package com.mvc.finance.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mvc.ProjectUtils;
import com.mvc.finance.model.ExpenseApplyItem;
import com.mvc.finance.service.ExpenseApplyItemManager;
import com.mvc.framework.util.AjaxUtils;

@Controller
@RequestMapping("expenseApplyItem")
public class ExpenseApplyItemController {
	private static final Logger	    LOGGER	= Logger.getLogger(ExpenseApplyItemController.class);
	@Autowired
	private ExpenseApplyItemManager	expenseApplyItemManager;

	@RequestMapping("list/{orderId}")
	public String list(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer orderId) throws Exception {
		List<ExpenseApplyItem> orderItems = expenseApplyItemManager.getExpenseApplyItemsByExpenseApplyId(orderId);
		int total = orderItems.size();
		StringBuilder rows = new StringBuilder(total);
		rows.append("{");
		rows.append("\"total\":\"").append(1);
		rows.append("\",\"page\":\"").append(1);
		rows.append("\",\"records\":\"").append(total);
		rows.append("\",\"rows\":[");
		for (int i = 0; i < total; i++) {
			rows.append(getOrderItemJson(orderItems.get(i)));
			if (i != total - 1) {
				rows.append(",");
			}
		}
		rows.append("]");
		rows.append("}");
		AjaxUtils.doJsonResponse(response, rows.toString());
		return null;
	}

	private String getOrderItemJson(ExpenseApplyItem orderItem) {
		StringBuilder row = new StringBuilder(80);
		row.append("{\"id\":\"").append(orderItem.getId());
		row.append("\",\"cell\":[\"").append(orderItem.getId());
		row.append("\",\"").append(orderItem.getObjectId());
		row.append("\",\"").append(ProjectUtils.toJsonField(orderItem.getReason()));
		row.append("\",\"").append(orderItem.getAmount());
		row.append("\",\"").append(ProjectUtils.toJsonField(orderItem.getReceiver()));
		row.append("\",\"").append(orderItem.getType());
		row.append("\",\"").append(ProjectUtils.toJsonField(orderItem.getBankAccountName()));
		row.append("\",\"").append(ProjectUtils.toJsonField(orderItem.getBankNo()));
		row.append("\",\"").append(ProjectUtils.toJsonField(orderItem.getBankName()));
		row.append("\",\"").append(ProjectUtils.toJsonField(orderItem.getBankBranchName()));
		row.append("\"]}");
		return ProjectUtils.replaceReturn(row.toString());
	}

	@RequestMapping("save")
	public String save(HttpServletRequest request, ModelMap modelMap, ExpenseApplyItem orderItem) throws Exception {
		String operation = request.getParameter("oper");
		boolean result = false;
		try {
			expenseApplyItemManager.saveBankStatementItem(orderItem, operation);
			result = true;
		} catch (Exception e) {
			LOGGER.error("save", e);
		}
		modelMap.addAttribute("status", result ? "success" : "error");
		return "jsonView";
	}
}
